package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartUpdateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.UpdateCartNumRequest;
import com.mall.shopping.dto.UpdateCartNumResponse;
import com.mall.user.annotation.Anonymous;
import io.swagger.models.auth.In;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @author pengyehua
 * @date 2020/12/21 16:05
 */

/**
 * 更新购物车
 */
@RestController
public class UpdateCartController {

    @Reference(check = false)
    ICartUpdateService cartUpdateService;

    @PutMapping("shopping/carts")
    public ResponseData updateCart(@RequestBody Map map){
        long userId = Long.parseLong((String) map.get("userId"));
        int productId = (int) map.get("productId");

        int productNum = (int) map.get("productNum");
        String checked = (String) map.get("checked");

        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(userId);
        request.setItemId((long)productId);
        request.setNum(productNum);
        request.setChecked(checked);
        UpdateCartNumResponse response = cartUpdateService.updateCart(request);

        if(!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(null);
    }
}
